<?php
# file that will process and save to database new user

# include of site functions
include_once $_SERVER["DOCUMENT_ROOT"].'Meet-n-go/php/includes.php'; 	// contains includes of all files with functions
session_start();					// starts session for current user
c_db_connect(); 					// connect to database

# make sure all data was entered

if(!isset($_POST['name']) || empty($_POST['name'])) i_set_msg_err('Empty name');
else{
	$name = c_escape($_POST['name']); // escape before doing something with it
}

if(!isset($_POST['email']) || empty($_POST['email'])) i_set_msg_err('Empty email');
else{
	$email = c_escape($_POST['email']); // escape before doing something with it
	
	# TODO check that it is e-mail
	
	# check that there are no such email
	$query = "SELECT email FROM user WHERE email='$email'";
	$result = c_db_query($query);
	$nr_rows = mysql_num_rows($result);
	if($nr_rows != 0) i_set_msg_err('This e-mail address is already registered');
}

if(!isset($_POST['password']) || empty($_POST['password'])) i_set_msg_err('Empty password');
else{
	$pass = c_escape($_POST['password']); // escape before doing something with it
}

if( !has_errors() ){
	
	$query = "INSERT INTO user (NAME, EMAIL, PASSWORD) VALUES ('$name', '$email', '$pass')";
	if(c_db_query($query)) i_set_msg_inf('You have been registered'); // registration successful
	else i_set_msg_err('Error creating new user'); // problem with database
	
}

c_db_disconnect(); 					// disconnect from database

# Where to go after processing
if( has_errors() ) 	$page_id = I_REGISTRATION_PAGE; // if there are erros, go back to registration page
else 				$page_id = I_MAIN_PAGE; // if there are no erros, go back to login page


# code that makes sure, this page is being redirected from afterwards
echo '<head><meta http-equiv="refresh" content="0; url=../../../index.php?id=' . $page_id . '" /></head>';


?>